Skip to content

Fix React.act infrastructure issue in jsdom component tests#326

Closed
brendanlong wants to merge 1 commit intomainfrom
claude/973012cc-ee1e-478e-b0e9-3eade127f504
Closed

Fix React.act infrastructure issue in jsdom component tests#326
brendanlong wants to merge 1 commit intomainfrom
claude/973012cc-ee1e-478e-b0e9-3eade127f504

Conversation

@brendanlong
Copy link
Copy Markdown
Owner

Summary

  • Fixes all 92 component tests failing with TypeError: React.act is not a function
  • Root cause: host environment has NODE_ENV=production, which causes react-dom/test-utils.js to load the production CJS bundle where React.act is not exported
  • Fix: add define: { 'process.env.NODE_ENV': JSON.stringify('test') } to vitest.component.config.ts so the development bundle is loaded

Fixes #320

Test plan

  • All 92 component tests pass (pnpm test:component)
  • All 361 unit tests pass (pnpm test:run)

🤖 Generated with Claude Code

The host environment has NODE_ENV=production, which causes React's CJS
entry point (react-dom/test-utils.js) to load the production bundle
where React.act is not exported. Add an explicit define to override
process.env.NODE_ENV to 'test' in the component test config so the
development bundle is loaded instead.

Fixes #320

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Vitest component configuration to define the NODE_ENV as 'test'. The reviewer suggests using the test.env configuration instead of the define property to ensure the environment variable is correctly handled across different access patterns, such as destructuring or bracket notation.

Comment on lines +5 to 8
define: {
'process.env.NODE_ENV': JSON.stringify('test'),
},
test: {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using define for process.env.NODE_ENV is a literal string replacement that only affects code using the exact process.env.NODE_ENV syntax. It may fail to cover cases where the environment variable is accessed via destructuring (e.g., const { NODE_ENV } = process.env) or bracket notation (e.g., process.env['NODE_ENV']).

For a more robust solution in Vitest, it is recommended to use the test.env configuration. Alternatively, setting the environment variable in the package.json test script (e.g., NODE_ENV=test vitest ...) ensures it is correctly set for the entire process, including the resolver and all dependencies.

Suggested change
define: {
'process.env.NODE_ENV': JSON.stringify('test'),
},
test: {
test: {
env: {
NODE_ENV: 'test',
},

@brendanlong brendanlong closed this Apr 7, 2026
@brendanlong brendanlong deleted the claude/973012cc-ee1e-478e-b0e9-3eade127f504 branch April 7, 2026 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix React.act infrastructure issue in jsdom component tests (React 19 + @testing-library/react)

2 participants